<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="申报主体" prop="companyName">
        <el-input
          v-model="queryParams.companyName"
          placeholder="请输入任务名称"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="项目状态" prop="xiangmuZhuangtai">
        <el-select
          v-model="queryParams.xiangmuZhuangtai"
          placeholder="请选择项目状态"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in proStatusOption"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="执行状态" prop="status">
          <el-select
            v-model="queryParams.status"
            placeholder="请选择执行状态"
            clearable
            style="width: 240px"
          >
            <el-option
              v-for="dict in dict.type.sys_common_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item> -->
      <el-form-item label="申报年份">
        <el-date-picker
          v-model="queryParams.shenbaoTime"
          style="width: 240px"
          value-format="yyyy"
          type="year"
          placeholder="请选择年份"
          :picker-options="pickOptions"
          @change="yearChange"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <!-- <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['monitor:job:add']"
          >新增</el-button
        > -->
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          >删除</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            @click="handleClean"
            v-hasPermi="['monitor:job:remove']"
          >清空</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['monitor:job:export']"
          >导出</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-close"
            size="mini"
            @click="handleClose"
          >关闭</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar> -->
    </el-row>

    <el-table v-loading="loading" :data="jobLogList">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="申报主体"
        width="80"
        align="center"
        :show-overflow-tooltip="true"
        prop="companyName"
      />
      <el-table-column
        label="项目名称"
        align="center"
        :show-overflow-tooltip="true"
        prop="xiangmuName"
      />
      <el-table-column
        label="申报年份"
        align="center"
        :show-overflow-tooltip="true"
        prop="shenbaoTime"
      >
        <!-- <template slot-scope="scope">
            <dict-tag :options="dict.type.sys_job_group" :value="scope.row.jobGroup"/>
          </template> -->
      </el-table-column>
      <el-table-column
        label="项目状态"
        align="center"
        :show-overflow-tooltip="true"
        prop="xiangmuZhuangtai"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.pro_status"
            :value="scope.row.xiangmuZhuangtai"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="核证减排量(tCO2e)"
        align="center"
        :show-overflow-tooltip="true"
        prop="hezhengJianpailiang"
      />
      <el-table-column
        label="项目详细信息"
        align="center"
        :show-overflow-tooltip="true"
        prop="status"
      >
        <template slot-scope="scope">
          <el-button @click="lookPdf(scope.row)" size="mini" type="primary" plain>
          {{scope.row.pdfName}}</el-button>
          <!-- <dict-tag :options="dict.type.sys_common_status" :value="scope.row.status"/> -->
        </template>
      </el-table-column>
      <el-table-column
        label="核证通过时间"
        align="center"
        :show-overflow-tooltip="true"
        prop="hezhengTongguoTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.hezhengTongguoTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="创建日期"
        align="center"
        prop="creatTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.creatTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="创建账户"
        align="center"
        :show-overflow-tooltip="true"
        prop="creatUser"
        width="180"
      >
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        fixed="right"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <!-- <el-button
              size="mini"
              type="text"
              icon="el-icon-view"
              @click="handleView(scope.row)"
              v-hasPermi="['monitor:job:query']"
            >详细</el-button> -->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            :disabled="scope.row.xiangmuZhuangtai == 4"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            :disabled="scope.row.xiangmuZhuangtai == 4"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 新增/修改 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="120px"
        label-position="right"
      >
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="申报主体" prop="companyName">
              <el-input
                v-model="form.companyName"
                placeholder="请输入申报主体"
                style="width: 200px"
              /> </el-form-item
          ></el-col>
          <el-col :span="12">
            <el-form-item label="项目名称" prop="xiangmuName">
              <el-input
                v-model="form.xiangmuName"
                placeholder="请输入项目名称"
                style="width: 200px"
              /> </el-form-item
          ></el-col>
          <el-col :span="12">
            <el-form-item label="申报时间" prop="shenbaoTime">
              <el-date-picker
                v-model="form.shenbaoTime"
                style="width: 200px"
                value-format="yyyy"
                type="year"
                placeholder="请选择日期"
              ></el-date-picker> </el-form-item
          ></el-col>
          <el-col :span="12">
            <el-form-item label="项目状态" prop="xiangmuZhuangtai">
              <el-select
                v-model="form.xiangmuZhuangtai"
                placeholder="请选择项目状态"
                clearable
                style="width: 200px"
              >
                <el-option
                  v-for="dict in proStatusOption"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select> </el-form-item
          ></el-col>
          <el-col :span="12">
            <el-form-item label="核证减排量" prop="hezhengJianpailiang">
              <el-input
                v-model="form.hezhengJianpailiang"
                placeholder="请输入核证减排量"
                style="width: 200px"
              /> </el-form-item
          ></el-col>
          <!-- <el-col :span="12">
            <el-form-item label="交易类型" prop="jiaoyiLeixing">
              <el-select
                v-model="form.jiaoyiLeixing"
                placeholder="请选择交易类型"
                clearable
                style="width: 200px"
              >
                <el-option
                  v-for="dict in jiaoyiLeixingOption"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select> </el-form-item
          ></el-col>
          <el-col :span="12">
            <el-form-item label="交易方式" prop="jiaoyiFangshi">
              <el-select
                v-model="form.jiaoyiFangshi"
                placeholder="请选择交易方式"
                clearable
                style="width: 200px"
              >
                <el-option
                  v-for="dict in jiaoyiFangshiOption"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select> </el-form-item
          ></el-col>
          <el-col :span="12">
            <el-form-item label="出售/购入方" prop="target">
              <el-input
                v-model="form.target"
                placeholder="请输入配额量"
                style="width: 200px"
              /> </el-form-item
          ></el-col> -->
          <el-col :span="12">
            <el-form-item label="核证通过时间" prop="hezhengTongguoTime">
              <el-date-picker
                v-model="form.hezhengTongguoTime"
                style="width: 200px"
                value-format="yyyy-MM-dd"
                type="date"
                placeholder="请选择通过时间"
              ></el-date-picker> </el-form-item
          ></el-col>
        </el-row>
        <!-- <el-form-item label="履约周期" prop="lvueZhouqi" class="cycleRange2">
          <el-date-picker
            v-model="form.startYear"
            style="width: 278px"
            value-format="yyyy"
            type="year"
            placeholder="选择开始日期"
            :picker-options="pickOptions"
          ></el-date-picker>
          <div style="margin: 0 10px">至</div>
          <el-date-picker
            v-model="form.endYear"
            style="width: 278px"
            value-format="yyyy"
            type="year"
            placeholder="选择结束日期"
            :picker-options="pickOptions"
          ></el-date-picker>
        </el-form-item> -->
        <el-form-item label="请选择文件">
          <el-upload
            class="upload-demo"
            action="https://jsonplaceholder.typicode.com/posts/"
            :on-change="handleChange"
            :file-list="fileList"
          >
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">
              只能上传jpg/png文件，且不超过500kb
            </div>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getJob } from "@/api/monitor/job";
import { listJobLog, delJobLog, cleanJobLog } from "@/api/monitor/jobLog";
import datas from "../../../../assets/jianpaiDengji.json";
import { listQuota, listCompany } from "@/api/demoApi/index";
export default {
  name: "JobLog",
  dicts: ["sys_common_status", "sys_job_group", "pro_status"],
  data() {
    return {
      // 项目文件
      fileList: [
        {
          name: "food.jpeg",
          url: "https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100",
        },
        {
          name: "food2.jpeg",
          url: "https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100",
        },
      ],
      // 项目状态
      proStatusOption: [],
      pickOptions: {
        onPick: ({ maxDate, minDate }) => {
          console.log(666, maxDate);
          console.log(777, minDate);
        },
        disabledDate(date) {
          // console.log(888,date.getFullYear());
          return date && date.getFullYear() > new Date().getFullYear();
        },
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 调度日志表格数据
      jobLogList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 表单参数
      form: {},
      defaultProps: {
        children: "children",
        label: "label",
      },
      // 表单校验
      rules: {
        companyName: [
          {
            required: true,
            message: "申报主体不能为空",
            trigger: "blur",
          },
        ],
        xiangmuName: [
          {
            required: true,
            message: "项目名称不能为空",
            trigger: "blur",
          },
        ],
        shenbaoTime: [
          {
            required: true,
            message: "交易时间不能为空",
            trigger: ["blur", "change"],
          },
        ],
        xiangmuZhuangtai: [
          {
            required: true,
            message: "项目状态不能为空",
            trigger: ["blur", "change"],
          },
        ],
        hezhengJianpailiang: [
          {
            required: true,
            message: "核证减排量不能为空",
            trigger: "blur",
          },
        ],
        hezhengTongguoTime: [
          {
            required: true,
            message: "核证通过时间不能为空",
            trigger: ["blur", "change"],
          },
        ],
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        jobName: undefined,
        jobGroup: undefined,
        status: undefined,

        companyName: "",
        xiangmuZhuangtai: "",
        shenbaoTime: "",
      },
    };
  },
  created() {
    this.getDicts("pro_status").then((response) => {
      this.proStatusOption = response.data;
    });
    // const jobId = this.$route.params && this.$route.params.jobId;
    // if (jobId !== undefined && jobId != 0) {
    //   getJob(jobId).then(response => {
    //     this.queryParams.jobName = response.data.jobName;
    //     this.queryParams.jobGroup = response.data.jobGroup;
    //     this.getList();
    //   });
    // } else {
    this.getList();
    // }
  },
  methods: {
    lookPdf(row) {
      // window.open('/pdfjs/web/viewer.html?file=' + row.pdfUrl)
      window.open(row.pdfUrl)
    },
    yearChange(val) {
      console.log("val", val);
    },
    // 文件上传
    handleChange(file, fileList) {
      this.fileList = fileList.slice(-3);
    },
    // 解析配额类型字典
    proStatusFormat(row, column) {
      return this.selectDictLabel(this.proStatusOption, row.xiangmuZhuangtai);
    },
    /** 查询调度日志列表 */
    getList() {
      this.loading = true;
      listQuota(this.addDateRange(this.queryParams)).then((response) => {
        // this.jobLogList = response.rows;
        // this.total = response.total;
        this.jobLogList = response.rows.slice(0, 10);
        this.total = 10;
        this.loading = false;
      });
    },
    // // 返回按钮
    // handleClose() {
    //   const obj = { path: "/monitor/job" };
    //   this.$tab.closeOpenPage(obj);
    // },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      // if (this.$refs.menu != undefined) {
      //   this.$refs.menu.setCheckedKeys([]);
      // }
      // (this.menuExpand = false),
      //   (this.menuNodeAll = false),
      //   (this.deptExpand = true),
      //   (this.deptNodeAll = false),
      this.form = {
        // roleId: undefined,
        // roleName: undefined,
        // roleKey: undefined,
        // roleSort: 0,
        // status: "0",
        // menuIds: [],
        // deptIds: [],
        // menuCheckStrictly: true,
        // deptCheckStrictly: true,
        // remark: undefined,

        companyName: "",
        xiangmuName: "",
        shenbaoTime: undefined,
        xiangmuZhuangtai: "",
        hezhengJianpailiang: undefined,
        hezhengTongguoTime: "",
        // jiaoyiTime:"",
        // startYear:'',
        // endYear:''
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },

    // // 多选框选中数据
    // handleSelectionChange(selection) {
    //   this.ids = selection.map((item) => item.jobLogId);
    //   this.multiple = !selection.length;
    // },
    // /** 详细按钮操作 */
    // handleView(row) {
    //   this.open = true;
    //   this.form = row;
    // },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      // this.getMenuTreeselect();
      this.open = true;
      this.title = "添加";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      console.log("row", row);
      // const roleId = row.roleId || this.ids;
      this.form = row;
      // const roleMenu = this.getRoleMenuTreeselect(roleId);
      // getRole(roleId).then((response) => {
      //   this.form = response.data;
      this.open = true;
      //   this.$nextTick(() => {
      //     roleMenu.then((res) => {
      //       let checkedKeys = res.checkedKeys;
      //       checkedKeys.forEach((v) => {
      //         this.$nextTick(() => {
      //           this.$refs.menu.setChecked(v, true, false);
      //         });
      //       });
      //     });
      //   });
      this.title = "修改";
      // });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.roleId != undefined) {
            // this.form.menuIds = this.getMenuAllCheckedKeys();
            // updateRole(this.form).then((response) => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.getList();
            // });
          } else {
            // this.form.menuIds = this.getMenuAllCheckedKeys();
            // addRole(this.form).then((response) => {
            this.$modal.msgSuccess("新增成功");
            this.open = false;
            this.getList();
            // });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const jobLogIds = this.ids;
      this.$modal
        .confirm('是否确认删除调度日志编号为"' + jobLogIds + '"的数据项？')
        .then(function () {
          return delJobLog(jobLogIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    // /** 清空按钮操作 */
    // handleClean() {
    //   this.$modal
    //     .confirm("是否确认清空所有调度日志数据项？")
    //     .then(function () {
    //       return cleanJobLog();
    //     })
    //     .then(() => {
    //       this.getList();
    //       this.$modal.msgSuccess("清空成功");
    //     })
    //     .catch(() => {});
    // },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "/monitor/jobLog/export",
        {
          ...this.queryParams,
        },
        `log_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
